var orderId="";

//相关业务逻辑
$(function(){
   page.bind();
   nameToIdUtil.getSelectValue("approvalUser","user/getDepartmentUserOnValid",{"deptName":"仓库"},1);
});

var table; 
var page={//页面总变量
		bind:function(){
			$("#coustomId").bind("keyup",function(){
				var orderId=$(this).val();
				$.ajax({
					url:'weiXin/getOrderDetail',
					data:{
						'orderId':orderId
					},
					success:function(data){
						if(data.result){
							data=data.data;
							var html=""
						    for(var i=0;i<data.length;i++){
						    	html+="<option value='"+data[i].orderId+"'>"+data[i].companyOrderId+" &nbsp;&nbsp;&nbsp;&nbsp;"+data[i].factoryId+" &nbsp;&nbsp;&nbsp;&nbsp;"+data[i].customerOrderId+"&nbsp;&nbsp;&nbsp;&nbsp;"+data[i].customerId+"</option>"
						    }
							$("#detailList").html(html);
						}
					}
				});
			});
			
			
			$("#purchaseOrder").bind("keyup",function(){
				var purchaseId=$(this).val();
				$.ajax({
					url:'order/getPurchaseMaterial',
					data:{
						'purchaseId':purchaseId
					},
					success:function(data){
						if(data.result){
							data=data.data;
							var html=""
								for(var i=0;i<data.length;i++){
									html+="<option value='"+data[i].purchaseId+"'>"+data[i].materialType+" &nbsp;&nbsp;&nbsp;&nbsp;"+data[i].color+" &nbsp;&nbsp;&nbsp;&nbsp;"+data[i].colorDescription+"</option>"
								}
							$("#purchaseOrderList").html(html);
						}
					}
				});
			});
			 
			 //输入采购入库数
			 $('#inWarehuseTable tbody').on('keyup', '.inCount', function () {
				 var count=$(this).val();
				 if(isNaN(Number(count))){
					 layAlert("采购入库数量必须是数字");
					 count=count.substring(0,count.length-1);
					 $(this).val(count);
					 return false;
				 }
				});
			//输入入库结算数
			$('#inWarehuseTable tbody').on('keyup', '.sumMoneyCount', function () {
				 var count=$(this).val();
				 if(isNaN(Number(count))){
					 layAlert("入库结算数量必须是数字");
					 count=count.substring(0,count.length-1);
					 $(this).val(count);
					 return false;
				 }
			});
			//输入入库单价
			$('#inWarehuseTable tbody').on('keyup', '.inCountPrice', function () {
				 var count=$(this).val();
				 if(isNaN(Number(count))){
					 layAlert("入库单价必须是数字");
					 count=count.substring(0,count.length-1);
					 $(this).val(count);
					 return false;
				 }
			});

             // 查询物料
			 $("#queryMaterialBtn").bind("click",function(){
				  var orderId=$("#coustomId").val();
				  var purchaseOrder=$("#purchaseOrder").val();
				  if(orderId=='' && purchaseOrder==''){
					  layAlert("订单号与采购单号至少填一个");
					  return false;
				  }
				  page.getPurchaseMaterialDate(orderId,purchaseOrder);
			 });
			 
			 
			 $("#savdInWarehouseBtn").bind("click",function(){
				 var approvalUser=$("select[name=approvalUser]").val();
				 if(approvalUser==""){
					 layAlert("请选择验收人");
					 return false;
				 }
				 //仓库
				 var warehouse = $("select[name=warehouse]").val();
				 if(warehouse==-1){
					 layAlert("请选择入库仓库");
					 return false;
				 } 
				 var array=new Array();
				 var requestUrl = "companyMaterial/savdInWarehouse";
				 $("input[name='checkIn']:checked").each(function(){
					var object=new Object();
					object.approvalUser=approvalUser;//验收人
					object.purchaseId=$(this).val();//采购单单号 
					object.type=$(this).nextAll().eq(0).val();//采购类型
					object.purchaseDetailId=$(this).nextAll().eq(1).val();//投料采购id
					object.materialCode=$(this).nextAll().eq(2).val();//物料代码
					object.count=$(this).parent().prevAll().eq(2).children("input").val();//采购入库数
					object.positions=$(this).parent().prevAll().eq(0).children("input").val();//仓位
					//新增入库单价,入库单位 
					object.inCountUnit = $(this).parent().prevAll().eq(7).find("select").val();//入库单位
					object.inCountPrice = $(this).parent().prevAll().eq(6).find("input").val();//入库单价 
					//新增入库结算数
					object.sumMoneyCount = $(this).parent().prevAll().eq(1).find("input").val(); 
					object.colorDescription = $(this).parents("td").prevAll().eq(11).text(); 
					if(warehouse!=1){//如果入库的不是原料仓,那么就带上仓位
						object.defaultWarehouse = warehouse;
						requestUrl = "companyMaterial/saveInOtherWarehouse";
					}  
					array.push(object);
				 });
				 
				 if(array.length<=0){
					 layAlert("请勾选入库行");
					 return false;
				 }
				 for(var i=0;i<array.length;i++){
					 if(array[i].count==''){
						 layAlert("入库数不能为空");
						 return false;
					 }
					 if(isNaN(Number(array[i].count))){
						 layAlert("采购入库数必须是数字");
						 return false; 
					 }
					 if(array[i].sumMoneyCount==''){
						 layAlert("入库结算数不能为空");
						 return false;
					 }
					 if(isNaN(Number(array[i].sumMoneyCount))){
						 layAlert("入库结算数必须是数字");
						 return false; 
					 }
					 if(array[i].inCountUnit==='' || array[i].inCountUnit==="请选择"){
						 layAlert("入库单位不能为空");
						 return false;
					 } 
					 if(isNaN(Number(array[i].inCountPrice))){
						 layAlert("入库单价只能是数字");
						 return false;
					 }
					 if(Number(array[i].inCountPrice)<=0){
//						 layAlert("入库单价不能小于等于0");
//						 return false; 
					 }else{  
						 if (!/^(0|[1-9]\d*)(\.\d{1,2})?$/.test(array[i].inCountPrice)) {
							layAlert("入库单价小数点后必须是1-2位"); 
							return false; 
						 }
					 }
				 } 
				ajaxUtil.ajaxRuquest(requestUrl,{"array":JSON.stringify(array)},function(data){
					 layAlert(data);
					 var purchaseId = $("#purchaseOrder").val();
					 var orderId = $("#coustomId").val(); 
					 page.getPurchaseMaterialDate(orderId,purchaseId);
				 }); 
			 });  
		}, 
		getPurchaseMaterialDate:function(orderId,purchaseOrder){
			table=ajaxUtil.dataTablesAjaxNotFxiaction("inWarehuseTable",10,"companyMaterial/getPurchaseMaterialDate",
					{"orderId":orderId,"purchaseOrder":purchaseOrder},[
					{"data":"type",render:function(data,type,row){
						if(data==1){
							return "订单采购";
						}
						if(data==2){
							return "库存采购"
						}
					}},  
					{"data":"purchaseId"},
					{"data":"materialName"},
					{"data":"color"},
					{"data":"colorDescription"},
					{"data":"model"},
					{"data":"unit"},
					{"data":"unitPrice"}, 
					{"data":"purchaseCount"}, 
					{"data":"unit",render:function(data,type,row){ 
						return "<select class='input-text unit'><option value='"+data+"'>"+data+"</option><select>";
					}},
					{"data":"unitPrice",render:function(data,type,row){
						return "<input type='text' class='input-text inCountPrice' style='width:60px'value='"+data+"'/>";
					}},   
					{"data":"factoryNo"},   
					{"data":"coustomId"},   
					{"data":"orderId"},  
					{"data":"count",render:function(data,type,row){
						return "<input type='text' class='input-text inCount' style='width:110px;' value='"+data+"'>";
					}},  
					{"data":"count",render:function(data,type,row){
						return "<input type='text' class='input-text sumMoneyCount' style='width:110px;' value='"+data+"'>";
					}},
					{"data":"orderId",render:function(data,type,row){
						return "<input type='text' class='input-text' >";
					}},
					{"data":"purchaseId",render:function(data,type,row){
						return  "<input type='checkbox' value='"+data+"' name='checkIn'>" +
								"<input type='hidden' value='"+row.type+"' class='type' />"+
								"<input type='hidden' value='"+row.purchaseDetailId+"' class='purchaseDetailId' />"+
								"<input type='hidden' value='"+row.materialCode+"' class='materialCode' />";
					}}
			],[9],function(td, cellData, rowData, row, col){
				ajaxUtil.ajaxRuquest("basicData/getCompanyUnit",null,function(data){
					if(data!==null && data.length>0){
						var html = "<option value=''>请选择</option>";
						var oldVal = rowData.unit;
						for(var i = 0;i<data.length;i++){
							html += "<option value='"+data[i]+"'>"+data[i]+"</option>"
						}
						if(oldVal===undefined || oldVal===null || oldVal.trim()===''){
							console.log($(td));
							console.log($(td).parent().find("select.unit"));
							$(td).parent().find("select.unit").html(html);
						}else{ 
							$(td).parent().find("select.unit").html(html);
							$(td).parent().find("select.unit").val(oldVal);
						}
					}
				});
			})
			 
			
			
		}
		
}
